package cn.dt70.service.impl;

import cn.dt70.dao.aqbInvestment.AqbInvestmentMapper;
import cn.dt70.pojo.AqbInvestment;
import cn.dt70.service.AqbInvestmentService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AqbInvestmentServiceImpl implements AqbInvestmentService {

    private static final Logger LOGGER = Logger.getLogger(AqbInvestmentServiceImpl.class);

    @Resource
    private AqbInvestmentMapper aqbInvestmentMapper;

    @Override
    public List<AqbInvestment> findById(Integer id) {
        List<AqbInvestment> InvestmentList = new ArrayList<>();

        try {
            InvestmentList = aqbInvestmentMapper.queryById(id);
            if(InvestmentList.size()<=0){
                LOGGER.debug("投资记录查询为空");
            }else{
                LOGGER.debug("查询成功！");
            }
        } catch (Exception e) {
            LOGGER.error("投资查询异常"+e.getMessage());
        }

        return InvestmentList;
    }

    @Override
    public List<AqbInvestment> findByIdInWeek(Integer id) {
        List<AqbInvestment> InvestmentList = new ArrayList<>();

        try {
            InvestmentList = aqbInvestmentMapper.queryByIdInWeek(id);
            if(InvestmentList.size()<=0){
                LOGGER.debug("一周投资记录查询为空");
            }else{
                LOGGER.debug("查询成功！");
            }
        } catch (Exception e) {
            LOGGER.error("一周投资查询异常"+e.getMessage());
        }

        return InvestmentList;
    }

    @Override
    public List<AqbInvestment> findByIdInMonth(Integer id) {
        List<AqbInvestment> InvestmentList = new ArrayList<>();

        try {
            InvestmentList = aqbInvestmentMapper.queryByIdInMonth(id);
            if(InvestmentList.size()<=0){
                LOGGER.debug("一个月投资记录查询为空");
            }else{
                LOGGER.debug("查询成功！");
            }
        } catch (Exception e) {
            LOGGER.error("一个月投资查询异常"+e.getMessage());
        }

        return InvestmentList;
    }

    @Override
    public List<AqbInvestment> findByIdInThreeMonth(Integer id) {
        List<AqbInvestment> InvestmentList = new ArrayList<>();

        try {
            InvestmentList = aqbInvestmentMapper.queryByIdInThreeMonth(id);
            if(InvestmentList.size()<=0){
                LOGGER.debug("三个月投资记录查询为空");
            }else{
                LOGGER.debug("查询成功！");
            }
        } catch (Exception e) {
            LOGGER.error("三个月投资查询异常"+e.getMessage());
        }

        return InvestmentList;
    }

    @Override
    public List<AqbInvestment> findByIdForTime(Integer id, Date begin, Date end) {
        List<AqbInvestment> InvestmentList = new ArrayList<>();

        try {
            InvestmentList = aqbInvestmentMapper.queryByIdForTime(id,begin,end);
            if(InvestmentList.size()<=0){
                LOGGER.debug("一段时间投资记录查询为空");
            }else{
                LOGGER.debug("查询成功！");
            }
        } catch (Exception e) {
            LOGGER.error("一段时间投资查询异常"+e.getMessage());
        }

        return InvestmentList;
    }

    @Override
    public List<AqbInvestment> findCreditorById(Integer id) throws Exception {
        List<AqbInvestment> InvestmentList = new ArrayList<>();

        try {
            InvestmentList = aqbInvestmentMapper.queryCreditorById(id);
            if(InvestmentList.size()<=0){
                LOGGER.debug("债权记录查询为空");
            }else{
                LOGGER.debug("查询成功！");
            }
        } catch (Exception e) {
            LOGGER.error("债权查询异常"+e.getMessage());
        }

        return InvestmentList;
    }

    @Override
    public boolean addInvestment(AqbInvestment aqbInvestment) throws Exception {
        int row = 0;

        try {
            row = aqbInvestmentMapper.addInvestment(aqbInvestment);
            if(row!=1){
                LOGGER.debug("添加投资记录失败");
                return false;
            }else{
                LOGGER.debug("添加投资记录成功");
                return true;
            }
        } catch (Exception e) {
            LOGGER.error("添加投资记录异常"+e.getMessage());
            return false;
        }

    }

    @Override
    public AqbInvestment findByInvestId(Integer investmentId) throws Exception {
        try {
            if(null!=aqbInvestmentMapper.queryInvestmentById(investmentId)){
                LOGGER.debug("查询成功！");
            }else{
                LOGGER.debug("查询失败！");
            }
        } catch (Exception e) {
            LOGGER.error("投资查询异常"+e.getMessage());
        }
        return aqbInvestmentMapper.queryInvestmentById(investmentId);
    }
}
